package com.example.erp.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.erp.entity.dto.ExcelDto;
import com.example.erp.service.IGoodsService;
import com.example.erp.service.IInoutBoundService;
import com.example.erp.service.ITypeService;
import com.example.erp.utils.AnalysisExcelUtils;
import com.example.erp.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/erp/excel")
@CrossOrigin
public class ExcelController {
    @Autowired
    private ITypeService typeService;
    @Autowired
    private IGoodsService goodsService;


    @RequestMapping("/upload")
    public JSONObject upload(MultipartFile file) throws IOException {
        List<ExcelDto>excelDtoList = AnalysisExcelUtils.analysisExcel(file);
        //首先生成类型和商品信息,再根据商品代码生成入库单
        typeService.insertType(excelDtoList);
        goodsService.insertGoods(excelDtoList);

        return JsonResult.successJson("文件上传成功");
    }
}
